package dialogos;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Time;
import java.util.Date;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.JButton;

import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import paneles.cTurnos;
import peluqueria.Peluqueria;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ConfigTurnosDialog extends javax.swing.JDialog {
	private JLabel lColorLibre;
	private JSpinner sHoraApertura;
	private JSpinner sHoraCierre;
	private JLabel lHoraCierre;
	private JLabel lColorColor;
	private JSpinner sMinApertura;
	private JLabel lHoraApertura;
	private JButton bCancelar;
	private JButton bAceptar;
	private JLabel lColorInhabilitado;
	private JButton bInhabilitado;
	private JButton bCorte;
	private JButton bColor;
	private JButton bLibre;
	private JSpinner sDuracionColor;
	private JLabel lDuracionColor;
	private JSpinner sDuracionCorte;
	private JLabel lDuracionCorte;
	private JSpinner sMinCierre;
	private JLabel lColorCorte;

	/**
	* Auto-generated main method to display this JDialog
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				ConfigTurnosDialog inst = new ConfigTurnosDialog(frame);
				inst.setVisible(true);
			}
		});
	}
	
	public ConfigTurnosDialog(JFrame frame) {
		super(frame,"Configuracion de turnos",true);
		initGUI();
	}
	
	private void initGUI() {
		try {
			FlowLayout thisLayout = new FlowLayout();
			thisLayout.setHgap(10);
			thisLayout.setVgap(10);
			getContentPane().setLayout(thisLayout);
			Properties p = new Properties();
			FileInputStream fis = new FileInputStream("propiedades.properties");
			p.load(fis);
			fis.close();
			long dcolor = Long.valueOf((String)p.get(new String("dturnocolor"))).longValue();
			long dcorte = Long.valueOf((String)p.get(new String("dturnocorte"))).longValue();
			Date hinicio = new Date(Long.valueOf((String)p.get(new String("hinicio"))).longValue());
			Date hcierre = new Date(Long.valueOf((String)p.get(new String("hcierre"))).longValue());
			Time t = new Time(0,0,0);
			Time dtcorte = new Time(dcorte+t.getTime());
			Time dtcolor = new Time(dcolor+t.getTime());
			{
				int r,g,b;
				lColorLibre = new JLabel();
				long valor = Long.valueOf(((String)p.get(new String("colorlibre")))).longValue();
				b = (int) (valor%256);
				long aux = valor/256;
				g = (int) (aux%256);
				aux = aux/(256);
				r = (int) (aux%256);
				lColorLibre.setBackground(new Color(r,g,b));
				getContentPane().add(lColorLibre);
				lColorLibre.setText("Color Turno Libre");
				lColorLibre.setPreferredSize(new java.awt.Dimension(180, 21));
				lColorLibre.setHorizontalAlignment(SwingConstants.CENTER);
				lColorLibre.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lColorLibre.setOpaque(true);
				lColorLibre.setFont(new java.awt.Font("Verdana",1,11));
				lColorLibre.setForeground(new java.awt.Color(255,255,255));
			}
			{
				bLibre = new JButton();
				getContentPane().add(bLibre);
				bLibre.setText("Seleccionar...");
				bLibre.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						JColorChooser chooser = new JColorChooser(lColorLibre.getBackground());
						Color c = JColorChooser.showDialog(ConfigTurnosDialog.this, "Seleccione un color", lColorLibre.getBackground());
						lColorLibre.setBackground(c);
						
					}
				});
			}
			{
				int r,g,b;
				lColorCorte = new JLabel();
				long valor = Long.valueOf(((String)p.get(new String("colorturnocorte")))).longValue();
				b = (int) (valor%256);
				long aux = valor/256;
				g = (int) (aux%256);
				aux = aux/(256);
				r = (int) (aux%256);
				lColorCorte.setBackground(new Color(r,g,b));
				getContentPane().add(lColorCorte);
				lColorCorte.setText("Color Turno Corte");
				lColorCorte.setPreferredSize(new java.awt.Dimension(180, 21));
				lColorCorte.setHorizontalAlignment(SwingConstants.CENTER);
				lColorCorte.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lColorCorte.setOpaque(true);
				lColorCorte.setFont(new java.awt.Font("Verdana",1,11));
				lColorCorte.setForeground(new java.awt.Color(255,255,255));
			}
			{
				bCorte = new JButton();
				getContentPane().add(bCorte);
				bCorte.setText("Seleccionar...");
				bCorte.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						Color c = JColorChooser.showDialog(ConfigTurnosDialog.this, "Seleccione un color", lColorCorte.getBackground());
						lColorCorte.setBackground(c);
					}
				});
			}
			{
				lColorColor = new JLabel();
				getContentPane().add(lColorColor);
				getContentPane().add(lColorColor);
				lColorColor.setText("Color Turno Color");				
				int r,g,b;
				long valor = Long.valueOf(((String)p.get(new String("colorturnocolor")))).longValue();
				b = (int) (valor%256);
				long aux = valor/256;
				g = (int) (aux%256);
				aux = aux/(256);
				r = (int) (aux%256);
				lColorColor.setBackground(new Color(r,g,b));
				lColorColor.setText("Color Turno Color");
				lColorColor.setPreferredSize(new java.awt.Dimension(180, 21));
				lColorColor.setHorizontalAlignment(SwingConstants.CENTER);
				lColorColor.setBorder(BorderFactory.createTitledBorder(""));
				lColorColor.setOpaque(true);
				lColorColor.setFont(new java.awt.Font("Verdana",1,11));
				lColorColor.setForeground(new java.awt.Color(255,255,255));
			}
			{
				bColor = new JButton();
				getContentPane().add(bColor);
				bColor.setText("Seleccionar...");
				bColor.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						Color c = JColorChooser.showDialog(ConfigTurnosDialog.this, "Seleccione un color", lColorLibre.getBackground());
						lColorColor.setBackground(c);
					}
				});
			}
			{
				int r,g,b;
				lColorInhabilitado = new JLabel();
				long valor = Long.valueOf(((String)p.get(new String("colorinhabilitado")))).longValue();
				b = (int) (valor%256);
				long aux = valor/256;
				g = (int) (aux%256);
				aux = aux/(256);
				r = (int) (aux%256);
				lColorInhabilitado.setBackground(new Color(r,g,b));
				getContentPane().add(lColorInhabilitado);
				lColorInhabilitado.setText("Color Turno No Disponible");
				lColorInhabilitado.setPreferredSize(new java.awt.Dimension(180, 21));
				lColorInhabilitado.setHorizontalAlignment(SwingConstants.CENTER);
				lColorInhabilitado.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lColorInhabilitado.setOpaque(true);
				lColorInhabilitado.setFont(new java.awt.Font("Verdana",1,11));
				lColorInhabilitado.setForeground(new java.awt.Color(255,255,255));
			}
			{
				bInhabilitado = new JButton();
				getContentPane().add(bInhabilitado);
				bInhabilitado.setText("Seleccionar...");
				bInhabilitado.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						Color c = JColorChooser.showDialog(ConfigTurnosDialog.this, "Seleccione un color", lColorInhabilitado.getBackground());
						lColorInhabilitado.setBackground(c);
					}
				});
			}
			{
				lHoraApertura = new JLabel();
				getContentPane().add(lHoraApertura);
				lHoraApertura.setText("Hora de Apertura");
				lHoraApertura.setHorizontalAlignment(SwingConstants.CENTER);
				lHoraApertura.setPreferredSize(new java.awt.Dimension(131, 21));
				lHoraApertura.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lHoraApertura.setSize(150, 21);
				lHoraApertura.setForeground(new java.awt.Color(0,0,0));
			}
			{
				Integer value = new Integer(hinicio.getHours()); 
				Integer min = new Integer(0);
				Integer max = new Integer(23); 
				Integer step = new Integer(1); 
				SpinnerNumberModel sAperturaModel = new SpinnerNumberModel(value, min, max, step);
				sHoraApertura = new JSpinner();
				getContentPane().add(sHoraApertura);
				sHoraApertura.setModel(sAperturaModel);
				sHoraApertura.setPreferredSize(new java.awt.Dimension(50, 21));
				sHoraApertura.setSize(30, 21);
				sHoraApertura.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				sHoraApertura.setBackground(new java.awt.Color(255,255,255));
			}
			{
				Integer value = new Integer(hinicio.getMinutes()); 
				Integer min = new Integer(0);
				Integer max = new Integer(59); 
				Integer step = new Integer(1); 
				SpinnerNumberModel sMinAperturaModel = new SpinnerNumberModel(value, min, max, step);
				sMinApertura = new JSpinner();
				getContentPane().add(sMinApertura);
				sMinApertura.setModel(sMinAperturaModel);
				sMinApertura.setPreferredSize(new java.awt.Dimension(50, 21));
				sMinApertura.setSize(30, 21);
				sMinApertura.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				sMinApertura.setBackground(new java.awt.Color(255,255,255));
			}
			{
				lHoraCierre = new JLabel();
				getContentPane().add(lHoraCierre);
				lHoraCierre.setText("Hora de Cierre");
				lHoraCierre.setHorizontalAlignment(SwingConstants.CENTER);
				lHoraCierre.setPreferredSize(new java.awt.Dimension(131, 21));
				lHoraCierre.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lHoraCierre.setSize(150, 21);
				lHoraCierre.setForeground(new java.awt.Color(0,0,0));
			}
			{
				Integer value = new Integer(hcierre.getHours()); 
				Integer min = new Integer(0);
				Integer max = new Integer(23); 
				Integer step = new Integer(1); 
				SpinnerNumberModel sCierreModel = new SpinnerNumberModel(value, min, max, step);
				sHoraCierre = new JSpinner();
				getContentPane().add(sHoraCierre);
				sHoraCierre.setModel(sCierreModel);
				sHoraCierre.setPreferredSize(new java.awt.Dimension(50, 21));
				sHoraCierre.setSize(30, 21);
				sHoraCierre.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				sHoraCierre.setBackground(new java.awt.Color(255,255,255));
			}
			{
				Integer value = new Integer(hcierre.getMinutes()); 
				Integer min = new Integer(0);
				Integer max = new Integer(59); 
				Integer step = new Integer(1); 
				SpinnerNumberModel sMinCierreModel = new SpinnerNumberModel(value, min, max, step);
				sMinCierre = new JSpinner();
				getContentPane().add(sMinCierre);
				sMinCierre.setModel(sMinCierreModel);
				sMinCierre.setPreferredSize(new java.awt.Dimension(50, 21));
				sMinCierre.setSize(30, 21);
				sMinCierre.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				sMinCierre.setBackground(new java.awt.Color(255,255,255));
			}
			{
				lDuracionCorte = new JLabel();
				getContentPane().add(lDuracionCorte);
				lDuracionCorte.setText("Duracion de Turnos Corte");
				lDuracionCorte.setHorizontalAlignment(SwingConstants.CENTER);
				lDuracionCorte.setPreferredSize(new java.awt.Dimension(190, 21));
				lDuracionCorte.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lDuracionCorte.setSize(150, 21);
				lDuracionCorte.setForeground(new java.awt.Color(0,0,0));
			}
			{
				Integer value = new Integer(dtcorte.getMinutes()); 
				Integer min = new Integer(0);
				Integer max = new Integer(200); 
				Integer step = new Integer(1); 
				SpinnerNumberModel sDuracionmodel = new SpinnerNumberModel(value, min, max, step);
				sDuracionCorte = new JSpinner();
				getContentPane().add(sDuracionCorte);
				sDuracionCorte.setModel(sDuracionmodel);
				sDuracionCorte.setPreferredSize(new java.awt.Dimension(50, 21));
				sDuracionCorte.setSize(30, 21);
				sDuracionCorte.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				sDuracionCorte.setBackground(new java.awt.Color(255,255,255));
			}
			{
				lDuracionColor = new JLabel();
				getContentPane().add(lDuracionColor);
				lDuracionColor.setText("Duracion de Turnos Color");
				lDuracionColor.setHorizontalAlignment(SwingConstants.CENTER);
				lDuracionColor.setPreferredSize(new java.awt.Dimension(190, 21));
				lDuracionColor.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lDuracionColor.setSize(150, 21);
				lDuracionColor.setForeground(new java.awt.Color(0,0,0));
			}
			{
				Integer value = new Integer(dtcolor.getMinutes()); 
				Integer min = new Integer(0);
				Integer max = new Integer(200); 
				Integer step = new Integer(1); 
				SpinnerNumberModel sDuracionColorModel = new SpinnerNumberModel(value, min, max, step);
				sDuracionColor = new JSpinner();
				getContentPane().add(sDuracionColor);
				sDuracionColor.setModel(sDuracionColorModel);
				sDuracionColor.setPreferredSize(new java.awt.Dimension(50, 21));
				sDuracionColor.setSize(30, 21);
				sDuracionColor.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				sDuracionColor.setBackground(new java.awt.Color(255,255,255));
			}
			{
				bAceptar = new JButton();
				getContentPane().add(bAceptar);
				bAceptar.setText("Aceptar");
				bAceptar.setPreferredSize(new java.awt.Dimension(90, 25));
				bAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						Properties p = new Properties();
						try {
							FileInputStream fis = new FileInputStream("propiedades.properties");
							p.load(fis);
							fis.close();
						} catch (FileNotFoundException e1) {
							JOptionPane.showMessageDialog(ConfigTurnosDialog.this,"El archivo de configuracion no se encuentra", "Error", JOptionPane.ERROR_MESSAGE);
						} catch (IOException e) {
						}
						Color c = lColorLibre.getBackground();
						p.put (new String("colorlibre"),
								new Long(c.getRed()*65536+c.getGreen()*256+c.getBlue()).toString());
						c = lColorCorte.getBackground();
						p.put(new String("colorturnocorte"),
								new Long(c.getRed()*65536+c.getGreen()*256+c.getBlue()).toString());
						c = lColorColor.getBackground();
						p.put(new String("colorturnocolor"),
								new Long(c.getRed()*65536+c.getGreen()*256+c.getBlue()).toString());
						c = lColorInhabilitado.getBackground();
						p.put(new String("colorinhabilitado"),
								new Long(c.getRed()*65536+c.getGreen()*256+c.getBlue()).toString());
						int hc = ((Integer)ConfigTurnosDialog.this.sHoraCierre.getValue()).intValue();
						int mc = ((Integer)ConfigTurnosDialog.this.sMinCierre.getValue()).intValue();
						
						int ha = ((Integer)ConfigTurnosDialog.this.sHoraApertura.getValue()).intValue();
						int ma = ((Integer)ConfigTurnosDialog.this.sMinApertura.getValue()).intValue();
						
						int dcorte = ((Integer)ConfigTurnosDialog.this.sDuracionCorte.getValue()).intValue();
						int dcolor = ((Integer)ConfigTurnosDialog.this.sDuracionColor.getValue()).intValue();
						
						Time corte = new Time(0,dcorte,0);
						Time color = new Time(0,dcolor,0);
						Time t = new Time(0,0,0);
						
						Time cierre = new Time(hc,mc,0);
						Time aper = new Time(ha,ma,0);
						
						p.put("hinicio", new Long(aper.getTime()).toString());
						p.put("hcierre",new Long(cierre.getTime()).toString());
						p.put("dturnocorte",new Long(corte.getTime()-t.getTime()).toString());
						p.put("dturnocolor",new Long(color.getTime()-t.getTime()).toString());
						try {
							FileOutputStream fos = new FileOutputStream("propiedades.properties");
							p.save(fos, "----- Sin Comentarios -----");
							fos.close();
							ConfigTurnosDialog.this.dispose();
						} catch (FileNotFoundException e) {
							JOptionPane.showMessageDialog(ConfigTurnosDialog.this,"El archivo de configuracion no se encuentra", "Error", JOptionPane.ERROR_MESSAGE);
						} catch (IOException e) {
							
						}
						
					}
				});
				Peluqueria.getInstance().getCalendario().getJTable1().setDefaultRenderer(Object.class, new cTurnos());
			}
			{
				bCancelar = new JButton();
				getContentPane().add(bCancelar);
				bCancelar.setText("Cancelar");
				bCancelar.setPreferredSize(new java.awt.Dimension(90, 25));
				bCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						ConfigTurnosDialog.this.dispose();
					}
				});
			}
			pack();
			this.setSize(330, 340);
			this.setLocationRelativeTo(null);
			this.setVisible(true);
		} catch (Exception e) {
			//e.printStackTrace();
			JOptionPane.showMessageDialog(Peluqueria.getInstance().getJFrame(), "Ha ocurrido un error con el archivo de configuracion.", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}

}
